<?
    include_once "include/ini.php";
	$i = 0;
	$dirname = 'forumimg';
	$j=0;
	$falsefiles = array();


   $topic_id = 1308;
   $cat_id = 42;

$query = "select moto_posts.post_time as pic_time, moto_posts.poster_id as pic_user_id,  moto_posts_text.post_text as post_text, moto_posts.post_username
   from moto_posts_text
   left outer join moto_posts
   on moto_posts_text.post_id = moto_posts.post_id
 where
moto_posts_text.post_id in (select moto_posts.post_id from moto_posts where topic_id='".$topic_id."') and
moto_posts_text.post_text like '%[url=%[img%]%[/img%][/url]%'";


   $res = array();
          $result = $db->sql_query($query);
          $i = 0;
          while ( $row = @mysql_fetch_assoc($result) )
          {
              foreach ( $row as $key => $value )
              {
                 $res[$i][$key] = $value;
              }
              $i++;
          }


$arr_res = array();
$arr_clear = array();
$j=0;
$arr_count = count($res);
	for ($i=0;$i<$arr_count;$i++)
{

  preg_match_all("[\[url=(.+?)\]\[img:[a-z0-9]+\](.+?)\[/img:[a-z0-9]+\]\[/url\]]is",$res[$i]['post_text'],$arr);

   $count = count($arr[1]);
   echo $count."<br>";
  for ($y=0;$y<$count;$y++)
{
//foreach ($arr as $row=>$value)
//{
//echo $row." ".$value;
//print_r ($value);
//echo "<br>";
if (preg_match("/.html/",$arr[1][$y],$arr2) > 0)
{
//   $lasts = ;
   $arr[1][$y] = substr($arr[1][$y],0,strrpos($arr[1][$y],"."));
//echo $arr[1][$y]."<br>";
}
if (substr_count($arr[2][$y],'ipicture.ru/') > 0)
{
   $url_arr = explode("/",$arr[2][$y]);
   $count_url = count($url_arr);
   unset ($url_arr[$count_url-2]);
   $arr[1][$y] = join('/',$url_arr);
}

  $query = "insert into moto_album set pic_filename='".$arr[1][$y]."', pic_thumbnail='".$arr[2][$y]."',
   pic_user_id='".$res[$i]['pic_user_id']."', pic_time='".$res[$i]['pic_time']."', pic_cat_id='".$cat_id."'";
   if (!in_array($arr[2][$y],$arr_clear))
   {
      $arr_clear[] = $arr[2][$y];
	  //$result = $db->sql_query($query);
   }
}

}

?>